package com.wswy.wzcx.ui.message

import android.os.Build
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Space
import android.widget.TextView
import com.blankj.utilcode.util.TimeUtils
import com.che.libcommon.api.page.PageAdapterWrapper
import com.che.libcommon.api.page.Pagination
import com.facebook.common.util.UriUtil
import com.facebook.drawee.view.SimpleDraweeView
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.loadLayout
import com.wswy.wzcx.utils.StringUtils


class MessageListAdapter(pagination: Pagination<MessageVO>?): PageAdapterWrapper<MessageVO, MessageListAdapter.AbsMessageViewHolder>(pagination) {

    override fun getItemViewType(position: Int): Int {
        return getItemData(position)?.type?:0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageListAdapter.AbsMessageViewHolder {
        return when(viewType){
            TYPE_USER -> {
                return UserMessageViewHolder(loadLayout(parent,R.layout.item_message_s))
            }
            TYPE_SYSTEM  -> {
                return SystemMessageViewHolder(loadLayout(parent,R.layout.item_message_u))
            }
            TYPE_SOCIAL -> {
                return SocialMessageViewHolder(loadLayout(parent,R.layout.item_message_social))
            }

            else -> NoneViewHolder(Space(parent.context))
        }
    }

    override fun onBindViewHolder(holder: MessageListAdapter.AbsMessageViewHolder, position: Int, data: MessageVO?) {

        data?.let {
            holder.bindData(data)
        }

    }


     abstract class AbsMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        abstract fun bindData(voData: MessageVO)
    }


    private class NoneViewHolder(itemView: View):AbsMessageViewHolder(itemView){
        override fun bindData(voData: MessageVO) {
        }

    }


    //服务消息
    private class UserMessageViewHolder(itemView: View) : AbsMessageViewHolder(itemView) {

        val tvTime: TextView = itemView.findViewById(R.id.tv_time)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_send_name)
        val tvContent: TextView = itemView.findViewById(R.id.tv_content)
        val sdv: ImageView = itemView.findViewById(R.id.sdv_header)

        override fun bindData(voData: MessageVO) {
            tvContent.text = voData.detail.content
            tvTitle.text = voData.detail.title
            tvTime.text = TimeUtils.millis2String(voData.detail.createAt * 1000)
        }
    }

    //活动消息
    private class SystemMessageViewHolder(itemView: View) : AbsMessageViewHolder(itemView) {

        val tvTime: TextView = itemView.findViewById(R.id.tv_time)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val tvContent: TextView = itemView.findViewById(R.id.tv_content)
        val tvAtion: TextView = itemView.findViewById(R.id.tv_action_detail)
        val sdv: ImageView = itemView.findViewById(R.id.sdv)
        val llContent: View = itemView.findViewById(R.id.ll_content)
        val flAction: View = itemView.findViewById(R.id.fl_action)

        override fun bindData(voData: MessageVO) {

            tvTime.text = TimeUtils.millis2String(voData.detail.createAt * 1000)
            tvTitle.text = voData.detail.title
            tvContent.text = voData.detail.content
            sdv.setImageURI(UriUtil.parseUriOrNull(voData.detail.thumbUrl))
        }
    }


    /**
     *
     */
    private class SocialMessageViewHolder(itemView: View) : AbsMessageViewHolder(itemView) {

        val imgAuthor: SimpleDraweeView = itemView.findViewById(R.id.img_author_icon)

        val tvAuthor: TextView = itemView.findViewById(R.id.tv_author)

        val tvTime: TextView = itemView.findViewById(R.id.tv_date)

        val tvTopicPreview: TextView = itemView.findViewById(R.id.tv_topic_preview)

        val imgTopicPreview: SimpleDraweeView = itemView.findViewById(R.id.img_topic_preview)

        val tvContent: TextView = itemView.findViewById(R.id.tv_msg_content)


        override fun bindData(voData: MessageVO) {

            tvTime.text = StringUtils.formatTime(voData.detail.createAt * 1000)

            if (voData.detail.title == "##点赞##"){

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    tvContent.text = null
                    tvContent.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_like_blue, 0, 0, 0)
                }else{
                    tvContent.setCompoundDrawables(null,null,null,null)
                    tvContent.text = voData.detail.title
                }
            }else{
                tvContent.setCompoundDrawables(null,null,null,null)
                tvContent.text = voData.detail.title
            }




            voData.socialMsg?.run {
                imgAuthor.setImageURI(avatar)
                tvAuthor.text = nickname


                if(!TextUtils.isEmpty(bbsImage)){
                    imgTopicPreview.visibility = View.VISIBLE
                    tvTopicPreview.visibility = View.GONE

                    imgTopicPreview.setImageURI(bbsImage)
                }else{
                    imgTopicPreview.visibility = View.GONE
                    tvTopicPreview.visibility = View.VISIBLE
                    tvTopicPreview.text = bbsTitle
                }


            }
        }
    }

}